<template>
  <section>
    <div class="sectionContent">
      <div class="sectionContentLeft">
        <ul>
          <li
            @click="dianj(key)"
            :id="keynum == key ? 'active' : ''"
            v-for="(item, key) in goods"
            :key="key"
          >
            <span>{{ item.name }}</span>
          </li>
        </ul>
      </div>
      <div ref="hezis" class="sectionContentRight">
        <div
          v-for="(item, key) in goods"
          :key="key"
          ref="foodsHeight"
          class="sectionContentRight1"
        >
          <span class="srcSpan">{{ item.name }}</span>
          <div
            v-for="(val, k) in item.foods"
            :key="k"
            class="sectionContentRight1-1"
          >
            <div @click="xiangxi(key, k)" id="srcImg">
              <img :src="val.icon" />
            </div>
            <div class="scr-content">
              <p @click="xiangxi(key, k)">{{ val.name }}</p>
              <p @click="xiangxi(key, k)">{{ val.description }}</p>
              <p @click="xiangxi(key, k)">
                月售{{ val.sellCount }}份&nbsp;&nbsp;好评率{{ val.rating }}%
              </p>
              <div class="price">
                <div class="price1">
                  <p
                    style="color: red; font-weight: 700; margin-right: 0.16rem"
                  >
                    <a
                      style="font-size: 0.2rem; color: red; font-weight: normal"
                      >￥</a
                    >{{ val.price }}
                  </p>
                  <p
                    v-if="val.oldPrice != ''"
                    style="text-decoration: line-through; font-weight: 700"
                  >
                    {{ "￥" + val.oldPrice }}
                  </p>
                </div>
                <div id="scr-content-button">
                  <span @click="reduce(key, k)" class="span1">-</span>
                  <input
                    ref="input"
                    v-model="num"
                    type="text"
                    value="0"
                    :name="key"
                    :id="k"
                  />
                  <span @click="add(key, k)" class="span2">+</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
export default {
  data() {
    return {
      keynum: 0,
      goods: [],
      foods: [],
      foodsArr: [],
      num: 0,
    };
  },
  methods: {
    reduce(key, k) {
      this.$refs.input.filter((item) => {
        if (item.name == key && item.id == k) {
          if (item.value > 0) {
            item.value = item.value - 1;
            console.log(item.value);
          }
        }
      });
    },
    add(key, k) {
      this.$refs.input.filter((item) => {
        if (item.name == key && item.id == k) {
          item.value = item.value * 1 + 1 * 1;
        }
      });
    },
    dianj(key) {
      this.keynum = key;
      let timer = null;
      if (!timer) {
        timer = setInterval(() => {
          if (this.$refs.hezis.scrollTop >= this.foodsArr[key]) {
            this.$refs.hezis.scrollTop -= 20;
          }
          if (this.$refs.hezis.scrollTop <= this.foodsArr[key]) {
            this.$refs.hezis.scrollTop += 20;
          }
          if (Math.abs(this.$refs.hezis.scrollTop - this.foodsArr[key]) < 15) {
            console.log("停止了");
            // + - 5px之间都会停止
            clearInterval(timer);
            timer = setTimeout(() => {
              console.log("执行了11");
              timer = null;
            }, 2000);
          }
        }, 1);
      }
    },
    // scrollFn(e){
    //     // console.log(e);
    //     let scrollTopNum = e.target.scrollTop;
    //     // console.log(scrollTopNum);
    //     for(let i=0;i<this.foodsArr.length-1;i++){
    //       if(typeof this.foodsArr[i+1]!='undefined'){
    //           if( scrollTopNum>= this.foodsArr[i] && scrollTopNum<=this.foodsArr[i+1]){
    //             console.log("??",i);
    //             this.keynum = i;
    //             break;
    //         }
    //       }else{
    //         this.keynum = this.foodsArr.length-1
    //       }

    //     }

    // },
    xiangxi(item, key) {
      this.$router.push({ name: "Minute", query: { foods: item, key: key } });
    },
  },
  created: async function () {
    let { data } = await this.$http.get("api/getdata");
    this.goods = data.goods;
    this.$nextTick(() => {
      let foodsHeight = this.$refs.foodsHeight; //第一步获取这个盒子然后
      let num = 0;
      foodsHeight.forEach((ele) => {
        //第二步获取这个盒子然后去遍历里面每一项盒子的高度
        this.foodsArr.push(num); //回到起始点是0，所以先向数组里面添加一个0进去 然后再和key值去匹配
        num += ele.offsetHeight;
      });
      console.log(this.foodsArr);
    });
  },
};
</script>

<style scoped>
section {
  width: 100%;
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -moz-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  overflow: hidden;
}

.sectionContent {
  display: flex;
  width: 100%;
  height: 100%;
  overflow-x: scroll;
}

.sectionContentLeft {
  width: 1.8rem;
  font-size: 0.24rem;
  font-weight: 200;
  height: 100%;
}
.sectionContentLeft ul {
  width: 100%;
  height: 100%;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -moz-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -moz-box-orient: vertical;
  -moz-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  /* overflow: hidden; */
  align-items: center;
  overflow-x: scroll;
}
.sectionContentLeft ul li {
  width: 100%;
  height: 2rem;
  background-color: #f3f5f7;
  padding-top: 0.24rem;
  padding-left: 0.24rem;
  overflow-x: scroll;
}
#active {
  background-color: red;
}
.sectionContentLeft ul li span {
  display: block;
  /* line-height: 0.28rem; */
  /* width: 1.12rem; */
  border-bottom: 1px solid rgba(7, 17, 27, 0.1);
  height: 100%;
}

.sectionContentRight {
  /* height: 100%; */
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -moz-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  background-color: #f3f5f7;
  overflow-x: scroll;
  position: relative;
}

.sectionContentRight1 .srcSpan {
  display: block;
  height: 0.52rem;
  font-size: 0.24rem;
  color: rgb(147, 153, 159);
  background-color: #f3f5f7;
  border-left: 2px solid #d9dde1;
  padding-left: 0.28rem;
  line-height: 0.52rem;
  width: 100%;
}

.sectionContentRight1-1 {
  width: 100%;
  /* height: 2rem; */
  padding: 0.36rem;
  overflow: hidden;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  margin-bottom: 0.02rem;
  background-color: #fff;
}
.sectionContentRight1-1 #srcImg {
  width: 1.15rem;
  height: 1.15rem;
  margin-right: 0.2rem;
}
.sectionContentRight1-1 #srcImg img {
  width: 1.15rem;
  height: 1.15rem;
}

.scr-content {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -moz-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  height: 100%;
}
.scr-content p:nth-of-type(1) {
  margin-top: 0.04rem;
  font-size: 0.28rem;
  color: rgb(7, 17, 27);
  line-height: 0.28rem;
}
.scr-content .price {
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -moz-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}
.scr-content .price1 {
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: start;
  -webkit-justify-content: start;
  -moz-box-pack: start;
  -ms-flex-pack: start;
  justify-content: start;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

#scr-content-button {
  width: 1.28rem;
  height: 0.4rem;
  font-size: 0.2rem;
}
#scr-content-button .span1,
#scr-content-button .span2 {
  display: block;
  width: 0.4rem;
  height: 0.4rem;
  line-height: 0.3rem;
  text-align: center;
  font-weight: 700;
  border: 2px solid #00a0dc;
  border-radius: 50%;
  outline: none;
  float: left;
}
#scr-content-button input {
  float: left;
  width: 0.48rem;
  height: 0.4rem;
  outline: none;
  border: 0;
  text-align: center;
  line-height: 0.4rem;
}

.bottom,
.scr-content p:nth-of-type(1),
.scr-content p:nth-of-type(2),
.scr-content p:nth-of-type(3) {
  margin-bottom: 0.16rem;
}

.gg,
.scr-content p:nth-of-type(2),
.scr-content p:nth-of-type(3) {
  font-size: 0.2rem;
  color: rgb(147, 153, 159);
  /* line-height: 0.2rem; */
}
</style>